package me.shurufa.widget.commentbar;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import me.shurufa.R;

/**
 * 作者 : andy
 * 日期 : 16/3/14 18:04
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 底部评论条
 */
public class SimpleSendCommentBar extends LinearLayout implements View.OnClickListener, TextWatcher {

    private LinearLayout mSendBtn;
    private EditText mContentEt;
    private String mCommentId;

    // 存储状态
    private SimpleOnSendButtonClickListener mListener;

    public SimpleSendCommentBar(Context context) {
        this(context, null);
    }

    public SimpleSendCommentBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleSendCommentBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SimpleSendCommentBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View contentView = View.inflate(getContext(), R.layout.layout_comment_bar, null);
        mContentEt = (EditText) contentView.findViewById(R.id.et_content);
        mSendBtn = (LinearLayout) contentView.findViewById(R.id.btn_send);
        mSendBtn.setOnClickListener(this);
        addView(contentView);
    }

    public void setCommentId(String commentId) {
        this.mCommentId = commentId;
    }

    public EditText getEditText() {
        return mContentEt;
    }

    public void setOnSendButtonClickListener(SimpleOnSendButtonClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_send:

                if (TextUtils.isEmpty(mContentEt.getText().toString())) {
                    return;
                }

                if (mListener != null) {
                    mListener.onSend(mCommentId, mContentEt.getText().toString());
                }

                break;
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        String content = mContentEt.getText().toString();
        mSendBtn.setClickable(TextUtils.isEmpty(content));
    }

    @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        mContentEt.requestFocus();
        return super.requestFocus(direction, previouslyFocusedRect);
    }

}
